'''
NBUitl - Nokia backup file utilities
Copyright (C) 2010 Xiong Huang

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

Project homepage: http://code.google.com/p/nbutil/

@author: Xiong Huang, xiong@moblet.cn

Created on Mar 26, 2010

'''
import sys
from nbutil.nbufile import NbuFile

if __name__ == '__main__':
    filename = 'Xiong_2010-03-25.nbu'
    if len(sys.argv) > 1:
        filename = sys.argv[1]

    nbuFile = NbuFile(filename)

    csv_filename = filename.replace('nbu', 'csv')
    csv = open(csv_filename, mode='w', encoding='GBK')
    print('Name, Cell', file=csv)

    for contact in nbuFile.getContacts():
        # Export contact's name and cell phone numbers to the csv file
        print('%s, %s' % (contact.getName(), ';'.join(contact.getCells())), file=csv)

        # generate vcard file respectively
        vcf = contact.getName() + '.vcf'
        print('Generating ' + vcf, end='')
        print(contact.vcard(), file=open(vcf, mode='w', encoding='GBK'))
        print(' - Done')
    
    csv.close()

    print('All contacts have also been exported to %s' % csv_filename)
